package com.guigu.liuzhireng.controller;

import com.guigu.aop.OpLog;
import com.guigu.liuzhireng.pojo.Menu;
import com.guigu.liuzhireng.pojo.Rolemenu;
import com.guigu.liuzhireng.service.RolemenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("Rolemenu.do")
public class RolemenuController {

    @Autowired
    RolemenuService rolemenuService;

    @RequestMapping("RolemenuAll")
    @ResponseBody
    public List<Menu> RolemenuAll(Integer roleid){
        return rolemenuService.RolemenuAll(roleid);
    }

    @OpLog(opModule = "角色菜单模块",operType = "角色赋予/收回菜单",operDesc = "此方法用于角色赋予/收回菜单")
    @RequestMapping("DelAdd")
    public String DelAdd(HttpServletRequest req){
        int roleid = Integer.parseInt(req.getParameter("roleid"));
        String[] authids = req.getParameterValues("menuid");

        List<Rolemenu> roleauthorizations = new ArrayList<>();
        for (String role : authids) {
            Rolemenu rolemenu = new Rolemenu();
            rolemenu.setRoleid(roleid);
            rolemenu.setMenuid(Integer.parseInt(role));
            roleauthorizations.add(rolemenu);
        }

        rolemenuService.DelAdd(roleauthorizations,roleid);
        return "/role/Role.html";
    }
}
